package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked","rawtypes"})
@RequiredArgsConstructor
public class CustomerServiceImpl implements CustomerService {

    private final CustomerInfoFeignClient customerInfoFeignClient;

    private final RedisTemplate redisTemplate;
    /**
     * 小程序授权登录
     * @param code
     * @return
     */
    @Override
    public String login(String code) {
        //todo 判断返回信息的过程由自定义结果解码器完成了
        /*//1.拿着code进行远程调用，返回用户id
        Result<Long> loginResult = customerInfoFeignClient.login(code);

        //2.判断如果返回失败了，返回错误提示
        Integer codeResult = loginResult.getCode();
        if (codeResult != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        //3.获取远程调用返回的用户id
        Long customerId = loginResult.getData();

        //4.判断返回id是否为空，如果为空，返回错误提示
        if (customerId == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }*/

        Long customerId = customerInfoFeignClient.login(code).getData();

        //5.生成token字符串
        String token = UUID.randomUUID().toString().replace("-", "");

        //6.把用户id放到Redis，设置过期时间
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,
                                            customerId.toString(),
                                            RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                                            TimeUnit.SECONDS);
        return token;
    }

    /**
     * 获取用户登录信息
     * @param userId
     * @return
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(Long userId) {

        //todo 查询用户信息的操作由aop完成了
        //1.根据token查询redis
       // String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);

        //2.判断用户id是否存在
       /* if (StringUtils.isEmpty(customerId)){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        //3.根据用户id进行远程调用，得到用户信息
        Result<CustomerLoginVo> customerLoginVoResult = customerInfoFeignClient.getCustomerLoginInfo(userId);

        Integer code = customerLoginVoResult.getCode();
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
        if (customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginVo;
        */
        return customerInfoFeignClient.getCustomerLoginInfo(userId).getData();
    }

    /**
     * 更新客户微信手机号码
     * @param updateWxPhoneForm
     * @return
     */
    @Override
    public Object updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
        return true;
    }
}
